<extend name="Public/forum"/>

<block name="body">
    <div>


        <php>
            $user = query_user(array('avatar128','uid','username','space_url','icons_html'), $post['uid']);
        </php>
        <div class="row">

            <div class="col-md-8">
                <h4>{$post.title|htmlspecialchars}</h4>
            </div>
            <div class="col-md-4">
                <p class="pull-right text-right">
                    <if condition="$post['uid']==is_login()">
                        <a class="btn btn-default" href="{:U('Index/edit',array('post_id'=>$post['id']))}">编辑</a>&nbsp;
                    </if>
                    <a class="btn btn-default" href="#reply_form">回复</a>&nbsp;
                    <php>
                        $hideStyle = 'display: none;';
                        $bookmarkStyle = $isBookmark ? $hideStyle : '';
                        $unbookmarkStyle = $isBookmark ? '' : $hideStyle;
                    </php>
                <span style="{$unbookmarkStyle}" id="unbookmark_button">
                    <a class="btn btn-default"
                       href="{:U('Index/doBookmark?add=0',array('post_id'=>$post['id']))}">取消收藏</a>&nbsp;
                </span>
                <span style="{$bookmarkStyle}" id="bookmark_button">
                    <a class="btn btn-default" href="{:U('Index/doBookmark',array('post_id'=>$post['id']))}">收藏</a>&nbsp;
                </span>
                </p>
            </div>
        </div>

        <hr/>

        <if condition="$showMainPost" style="position: relative">
            <div class="row" style="position: relative">

                <div class="ribbion-green">
                    <a>楼主</a></div>
                <div class="col-md-2">
                    <p>
                        <a href="{$user.space_url}" ucard="{$user.uid}"><img src="{$user.avatar128}"
                                                                             class="avatar-img"/></a>
                    </p>

                    <p class="text-center">
                        <a href="{$user.space_url}" ucard="{$user.uid}">{$user.username|htmlspecialchars}</a>
                        {$user.icons_html}
                    </p>
                </div>
                <div class="col-md-1">
                    &nbsp;
                </div>
                <div class="col-md-9 post_content">
                    <div style="min-height: 10em;">
                        {$post.content}
                        <br/>
                        <if condition="$post['create_time'] != $post['update_time']">
                            <p class="text-muted">
                                [ 于 {$post['update_time']|time_format} 编辑 ]
                            </p>
                        </if>
                    </div>
                    <p class="pull-right text-muted">
                        发表于 {$post.create_time|time_format}
                    </p>
                </div>
            </div>

            <hr/>
        </if>

        <volist name="replyList" id="reply">
            <div class="row" style="position: relative">

                <eq name="reply.uid" value="$post['uid']">
                    <div class="ribbion-green">
                        <a>楼主</a></div>
                </eq>

                <div class="col-md-2">
                    <p class="text-center">
                        <a ucard="{$reply.uid}" href="{$reply.user.space_url}"><img src="{$reply.user.avatar128}"
                                                                             class="avatar-img"/></a>
                    </p>

                    <p class="text-center">
                        <a ucard="{$reply.uid}" href="{$reply.user.space_url}">{$reply.user.username}</a>
                        {$user.icons_html}
                    </p>
                </div>
                <div class="col-md-1">&nbsp;</div>
                <div class="col-md-9">
                    <div style="min-height: 10em;" class="post_content">
                        {$reply.content}
                        <br/>
                    </div>
                    <p class="pull-right text-muted">
                        发表于 {$reply.create_time|time_format}
                    </p>
                </div>
            </div>
            <hr/>
        </volist>

        <div class="row">
            <div class="col-md-12">
                <pull class="pull-right">
                    {:getPagination($replyTotalCount)}
                </pull>
            </div>
        </div>

        <br/>

        <div class="row">
            <div class="col-md-2">

                <p>
                    <a href="{$user.space_url}" ucard="{$user.uid}"><img src="{$user.avatar128}"
                                                                         class="avatar-img"/></a>
                </p>
            </div>
            <div class="col-md-9">
                <form id="reply_form" action="{:U('doReply',array('post_id'=>$post['id']))}">
                    <h4>发表回复</h4>

                    <p>
                        <textarea name="reply_content" placeholder="说点什么吧～" class="form-control"
                                  style="height: 6em;"></textarea>
                        {:hook('documentEditFormContent',array('name'=>'reply_content'))}
                    </p>

                    <p class="pull-right">
                        <input type="submit" id="reply_button" class="btn btn-primary" value="发表"/>
                    </p>
                </form>
            </div>
        </div>
    </div>
</block>

<block name="script">
    <script>
        $(function () {
            $('#reply_form').submit(function (e) {
                //获取评论内容
                var content = $('[name=reply_content]').val();
                var action = $('#reply_form').attr('action');

                //提交到服务器
                var replyButton = $('#reply_button');
                replyButton.attr('class', 'btn btn-primary disabled');
                replyButton.val('正在发表...');
                $.post(action, {content: content}, function (a) {
                    if (a.status) {
                        location.href = location.href;
                    } else {
                        replyButton.val(a.info);
                        replyButton.attr('class', 'btn btn-danger');
                    }
                });

                //取消默认操作
                e.preventDefault();
                return false;
            });
        });

        //点击收藏/取消收藏按钮
        $(function () {
            $('#bookmark_button a, #unbookmark_button a').click(function (e) {
                //发送AJAX请求
                var button = $(this);
                var href = button.attr('href');
                var originalClass = $(this).attr('class');
                button.attr('class', 'btn btn-default disabled');
                $.post(href, {}, function (a) {
                    button.attr('class', originalClass);
                    if (a.status) {
                        switchButtonVisibility();
                    }
                });

                //取消默认操作
                e.preventDefault();
                return false;
            });

            function switchButtonVisibility() {
                switchVisibility('#bookmark_button');
                switchVisibility('#unbookmark_button');
            }

            function switchVisibility(css) {
                var element = $(css);
                if (element.is(':visible')) {
                    element.hide();
                } else {
                    element.show();
                }
            }
        })
    </script>
    <style>
        .forum-first-block {
            background: white;
            box-shadow: 0 0 5px rgb(204, 204, 204);
            -moz-box-shadow: 0 0 5px #CCCCCC;
            -khtml-box-shadow: 0 0 5px #CCCCCC;
        }
    </style>
</block>
